package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static cn.wolfcode.wolf2w.util.JsonResult.success;

@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IRedisService redisService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/get")
    public Object get(String id){
        return userInfoService.selectOne(id);
    }

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        boolean checkResult=userInfoService.checkPhone(phone);
        return !checkResult;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        //1、获取手机号码
        //2、生成4位的验证码
        String code1=userInfoService.getCode();
        //3、模拟用信息发送验证码到用户手机中;
        //4、将手机号码和验证码储存在redis中
        redisService.saveRedis(phone, code1);
        return JsonResult.success();
    }

    @PostMapping("/regist")
    public Object regist(String phone, String nickname, String password, String rpassword, String verifyCode){
        //获取页面请求携带的验证码
        //通过请求的对象获取redis中的验证码
        String code1 = redisService.getRedis(phone);
        //然后比较请求携带的验证码和redis中的验证码是否相同
        if(code1.equals(verifyCode)){
            return JsonResult.success();
        }else{
            return null;
        }

    }

}
